package com.home.ty_smart_home_example

import android.Manifest
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.location.LocationManager
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Process
import android.provider.Settings
import android.text.TextUtils
import android.util.Log
import androidx.annotation.NonNull
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.PermissionChecker
import com.home.ty_smart_home.LCTySmartHomeTool
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant


class MainActivity: FlutterActivity() {

    val GPS_REQUEST_CODE = 1315 //开启GPS权限
    val CODE_FOR_LOCATION_PERMISSION = 222

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        LCTySmartHomeTool(this, flutterEngine.dartExecutor.binaryMessenger)

//        showLocationError()
    }
    
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private fun isWifiDisabled(): Boolean {
        val mWifiManager = this.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        return !mWifiManager.isWifiEnabled
    }

    fun showLocationError() {
        val ssid: String = ""
        if (!TextUtils.isEmpty(ssid)) {
            return
        } else {
            if (isWifiDisabled()) {
                return
            }
            if (!checkSystemGPSLocation()) {
                return
            } else {
                checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION, CODE_FOR_LOCATION_PERMISSION)
            }
        }
    }

    fun checkSystemGPSLocation(): Boolean {
        val isOpen: Boolean
        val locationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        if (!isOpen) {
            AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setTitle("Tip")
                    .setMessage("Enable phone location permission to find nearby Wi-Fi devices")
                    .setNegativeButton("cancel", null)
                    .setPositiveButton("setup", DialogInterface.OnClickListener { dialogInterface, i ->

                        // 跳转到手机原生设置页面
                        val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                        this.startActivityForResult(intent, GPS_REQUEST_CODE)
                        dialogInterface.dismiss()
                    })
                    .show()
        }
        return isOpen
    }

    fun checkSinglePermission(permission: String, resultCode: Int): Boolean {
        val hasPermission: Boolean
        if (Build.VERSION.SDK_INT < 23) {
            return true
        } else {
            hasPermission = hasPermission(permission)
        }
        if (!hasPermission) {
            // 开始请求权限
            ActivityCompat.requestPermissions(this, arrayOf(permission),
                    resultCode)
            return false
        }
        return true
    }

    fun hasPermission(permission: String?): Boolean {
        var targetSdkVersion = 0
        try {
            val info: PackageInfo = this.packageManager.getPackageInfo(
                    this.packageName, 0)
            targetSdkVersion = info.applicationInfo.targetSdkVersion
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        var result = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Android 6.0 开始动态申请权限
            result = if (targetSdkVersion >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                (ContextCompat.checkSelfPermission(this, permission!!)
                        == PackageManager.PERMISSION_GRANTED)
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                (PermissionChecker.checkSelfPermission(this, permission!!)
                        == PermissionChecker.PERMISSION_GRANTED)
            }
        }
        return result
    }


}
